package com.chengyu.core.controller;




import com.chengyu.core.utils.AmapUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;



@RestController
@RequestMapping("/location")
public class LocationController extends UserBaseController {
    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(LocationController.class);

    @Autowired
    private AmapUtil amapUtil;

    public static final Logger log = LoggerFactory.getLogger(LocationController.class);


    //根据经纬度获取地址
    @ApiOperation(value = "获取托运订单详情")
    @ResponseBody
    @RequestMapping(value = {"/getAddress"}, method = RequestMethod.GET)
    public String getAddress(String longitude, String lat) {
        try {
            System.out.println(longitude);
            System.out.println(lat);
            String address = amapUtil.getAMapByLngAndLat(longitude, lat);
            return  address;
        } catch (Exception e) {
            e.printStackTrace();
            return "Aaa";
        }
    }
    //根据地址获取经纬度
    @ApiOperation(value = "获取托运订单详情")
    @ResponseBody
    @RequestMapping(value = {"/getLonLat"}, method = RequestMethod.GET)
    public String getLonLat( String address) {
        try {
            String result = amapUtil.getLonLat(address);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
            return  "aaa";
        }
    }
    //通过地址算出两个地址之间的距离
    @GetMapping("/getDistanceByAddress/{start}/{end}")
    public Long getDistanceByAddress(@PathVariable String start,@PathVariable String end){
        return amapUtil.getDistanceByAddress(start,end);
    }




//    @Autowired
//    private LocationUtils locationUtils;
//
//    /**
//     * 地理编码
//     * http://localhost:2080/location/get?address=大同市平城区南环路&city=大同
//     * @param address
//     * @param city
//     * @return
//     */
//    @GetMapping("/get")
//    public JSONObject getLocation(@RequestParam String address, @RequestParam String city){
//
//        Map<String, String> params = new HashMap<>();
//        params.put("address", address);
//        params.put("city", city);
//        logger.info("获取地理编码,请求的参数为:{}", params);
//        JSONObject map = locationUtils.getLocation(params);
//        logger.info("获取地理编码,返回的请求结果为:{}", map);
//        return map;
//    }
//
//    /**
//     * 逆地理编码
//     * http://localhost:2080/location/getCounter?location=113.300038,40.063278
//     * @param location
//     * @return
//     */
//    @GetMapping("/getCounter")
//    public JSONObject getCounterLocation(@RequestParam String location){
//
//        Map<String, String> params = new HashMap<>();
//        params.put("location", location);
//        logger.info("获取逆地理编码,请求的参数为:{}", params);
//        JSONObject map = locationUtils.getCounterLocation(params);
//        logger.info("获取逆地理编码,返回的请求结果为:{}", map);
//        return map;
//    }

}

